package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.Timer;


/**
 *  Project :  TeamProject
 *  Package :  gui
 *  File    :  Timer.java
 *  Name    :  Tyler Daines
 *  Date    :  Dec 1, 2013
 **/
public class TimerPanel extends JPanel implements ActionListener
{
	private JButton startButton, resetButton, stopButton;
	private JTextPane hourPane , minutePane, secondPane;
	private JLabel colon1, colon2; 
	private Timer timer;
	private int hours, minutes, seconds;
	private Font timeFont;
	private Clip clip;
	private AudioInputStream audioInputStream;
	private int soundPlayed = 0;
	
	
	/**
	 * Name       : TimerPanel
	 * Purpose    : Constructor
	 * Parameters : 
	 **/
	public TimerPanel()
	{
		setLayout(null);
		timer = new Timer(1000, this);	
		hours = 0;
		minutes = 0;
		seconds = 0;
		
		initPane();
		initButtons();
	}

	/**
	* Name       : initPane
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	**/
	private void initPane()
	{
		
		timeFont = new Font("Times New Roman", Font.BOLD, 64);
		hourPane = new JTextPane();
		hourPane.setText(displayHours());
		hourPane.setFont(timeFont);
		hourPane.setBounds(200, 200, 100, 75);
		
		colon1 = new JLabel(":");
		colon1.setFont(timeFont);
		colon1.setBounds(295, 200, 25, 75);
		
		minutePane = new JTextPane();
		minutePane.setText(displayMinutes());
		minutePane.setFont(timeFont);
		minutePane.setBounds(310, 200, 100, 75);
		
		colon2 = new JLabel(":");
		colon2.setFont(timeFont);
		colon2.setBounds(405, 200, 25, 75);
		
		secondPane = new JTextPane();
		secondPane.setText(displaySeconds());
		secondPane.setFont(timeFont);
		secondPane.setBounds(420, 200, 100, 75);
		
		
		add(hourPane);
		add(colon1);
		add(minutePane);
		add(colon2);
		add(secondPane);
		
	}

	/**
	* Name       : initButtons
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	**/
	private void initButtons()
	{
		startButton = new JButton("Start");
		startButton.setBounds(200, 300, 100, 25);
		startButton.addActionListener(this);
		
		stopButton = new JButton("Stop");
		stopButton.setBounds(300, 300, 100, 25);
		stopButton.addActionListener(this);
		
		resetButton = new JButton("Reset");
		resetButton.setBounds(400, 300, 100, 25);
		resetButton.addActionListener(this);
		
		add(startButton);
		add(stopButton);
		add(resetButton);
		
	}


	/**
	* Name       : playSound
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	**/
	private void playSound()
	{
		try
		{
			clip = AudioSystem.getClip();
			audioInputStream = AudioSystem.getAudioInputStream(getClass().getResource("/resources/AlarmTone.wav"));
			clip.open(audioInputStream);
			clip.start();
		}
		catch (Exception e)
		{
			System.err.println(e.getMessage());
		}
		
	}




	/**
	* Name       : displayHours
	* Purpose    : 
	* Parameters : 
	* Returns    : String
	**/
	private String displayHours()
	{
		return String.format("%02d", hours);
	}
	
	/**
	* Name       : displayHours
	* Purpose    : 
	* Parameters : 
	* Returns    : String
	**/
	private String displayMinutes()
	{
		return String.format("%02d", minutes);
	}
	
	/**
	* Name       : displayHours
	* Purpose    : 
	* Parameters : 
	* Returns    : String
	**/
	private String displaySeconds()
	{
		return String.format("%02d", seconds);
	}

	/**
	* Name       : decreaseTime
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	**/
	private void decreaseTime()
	{
		
		if(seconds > 0)
		{
			seconds--;
		}
		else if(minutes > 0)
		{
			minutes--;
			seconds = 59;
		}
		else if(hours > 0)
		{
			hours--;
			minutes = 59;
			seconds = 59;
		}
		else if(soundPlayed < 3)
		{
			playSound();
			soundPlayed++;
		}
		else
		{
			soundPlayed = 0;
			timer.stop();
		}
		
	}




	/**
	* Name :actionPerformed
	*@see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	**/
	@Override
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == startButton)
		{
			hours = Integer.parseInt(hourPane.getText());
			minutes = Integer.parseInt(minutePane.getText());
			seconds = Integer.parseInt(secondPane.getText());
			timer.start();
		}
		
		if(e.getSource() == stopButton)
		{
			timer.stop();
		}
		
		if(e.getSource() == resetButton)
		{
			timer.stop();
			hours = 0;
			minutes = 0;
			seconds = 0;
			hourPane.setText(displayHours());
			minutePane.setText(displayMinutes());
			secondPane.setText(displaySeconds());
		}
		
		if(e.getSource() == timer)
		{
			decreaseTime();
			hourPane.setText(displayHours());
			minutePane.setText(displayMinutes());
			secondPane.setText(displaySeconds());
			
		}
	}
	
	/**
	* Name :getName
	*@see java.awt.Component#getName()
	**/
	@Override
	public String getName()
	{
		// TODO Auto-generated method stub
		return "Timer";
	}
	
	
}
